/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import Persistence.UserFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
import servidorped.User;

/**
 *
 * @author Micosil
 */
public class login implements Command {

    @Override
    public boolean execute(String[] stringList, User user) {
        if (user.getUserName() != null) {            
            user.getUserSender().setToSendln("You have already logged in as " + user.getUserName());
            return false;
        }
        User iWantToBeThisGuy = UserFactory.getUserPersistence().getUserByName(stringList[1]);
        if (iWantToBeThisGuy == null){
            user.getUserSender().setToSend("User does not exist.");
            return false;
        }
        if (stringList[2].equals(iWantToBeThisGuy.getPassword())){
            user.loginAs(iWantToBeThisGuy);
            servidorped.UserListFactory.getList().connectUser(user);
            user.getUserSender().setToSend("Logged as " + user.getUserName());
            Logger.getGlobal().log(Level.INFO, "User {0} logged in.", user.getUserName());
            return true;
        }
        user.getUserSender().setToSend("Login failed.");
        Logger.getGlobal().log(Level.INFO, "Failed to log in as {0}.", iWantToBeThisGuy.getUserName());
        return false;
    }


    }
    

